package cn.edu.xmu.oomall.customer.dao;

import cn.edu.xmu.oomall.core.util.JacksonUtil;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.mapper.AddressPoMapper;
import cn.edu.xmu.oomall.customer.model.bo.Address;
import cn.edu.xmu.oomall.customer.model.po.AddressPo;
import cn.edu.xmu.oomall.customer.model.po.AddressPoExample;
import cn.edu.xmu.oomall.customer.model.vo.AddressVo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;

import java.util.List;

import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:14:08
 */
public class AddressDao {
    @Autowired(required = false)
    private AddressPoMapper addressPoMapper;

    public ReturnObject addNewAddress(Long creatorId, String creatorName, Address address) {
        AddressPo addressPo = Common.cloneVo(address, AddressPo.class);
        addressPo.setBeDefault((byte) (address.getBeDefault().equals(false)?0:1));
        Common.setPoCreatedFields(addressPo, creatorId, creatorName);

        try {
            addressPoMapper.insertSelective(addressPo);
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        return new ReturnObject<>(addressPo);
    }

    public ReturnObject getAllAddress(Long customerId) {
        AddressPoExample addressPoExample = new AddressPoExample();
        AddressPoExample.Criteria criteria = addressPoExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        List<AddressPo> addressPos = addressPoMapper.selectByExample(addressPoExample);
        return new ReturnObject(addressPos);
    }

    public ReturnObject setAddressDefault(Long customerId, String customerName, Long addressId) {
        try {
            AddressPo addressPo = addressPoMapper.selectByPrimaryKey(addressId);
            if (null == addressPo) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
        } catch (Exception e) {
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
        System.out.println();

        AddressPoExample addressPoExample = new AddressPoExample();
        AddressPoExample.Criteria criteria = addressPoExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andBeDefaultEqualTo((byte)1);
        try {
            List<AddressPo> addressPos = addressPoMapper.selectByExample(addressPoExample);
            if (!addressPos.isEmpty()) {
                AddressPo tempPo = addressPos.get(0);
                tempPo.setBeDefault((byte)0);
                Common.setPoModifiedFields(tempPo, customerId, customerName);
                addressPoMapper.updateByPrimaryKeySelective(tempPo);
            }

            AddressPo addressPo = new AddressPo();
            addressPo.setCustomerId(customerId);
            addressPo.setBeDefault((byte)1);
            addressPo.setId(addressId);
            Common.setPoModifiedFields(addressPo, customerId, customerName);
            addressPoMapper.updateByPrimaryKeySelective(addressPo);
            return new ReturnObject<>(ReturnNo.OK);
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject modifyAddress(Long customerId, String customerName, Long addressId, AddressVo addressVo) {
        AddressPo addressPo = (AddressPo) Common.cloneVo(addressVo, AddressPo.class);
        addressPo.setCustomerId(customerId);
        addressPo.setId(addressId);
        Common.setPoModifiedFields(addressPo, customerId, customerName);
        try {
            addressPoMapper.updateByPrimaryKeySelective(addressPo);
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        return new ReturnObject<>(ReturnNo.OK);
    }

    public ReturnObject deleteAddress(Long customerId, Long addressId) {
        AddressPoExample addressPoExample = new AddressPoExample();
        AddressPoExample.Criteria criteria = addressPoExample.createCriteria();
        criteria.andIdEqualTo(addressId);
        criteria.andCustomerIdEqualTo(customerId);

        try {
            if (addressPoMapper.deleteByExample(addressPoExample) == 0) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }

        return new ReturnObject<>(ReturnNo.OK);
    }
}
